﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ModularFixture.CApMFLocatableGridRow
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EMPAPPSERVICESLib;
using EMPMODELLib;
using EMPTYPELIBRARYLib;
using System.Collections;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ModularFixture;

internal class CApMFLocatableGridRow : CApMFGridRow
{
  private bool m_basicUnitStructure;
  private TxTransformation m_location;
  private EmpContext m_context;
  private int m_loadedId;

  internal CApMFLocatableGridRow(int prototypeId, int instanceId, int loadedId)
  {
    this.m_basicUnitStructure = false;
    this.m_location = new TxTransformation();
    this.m_prototypeId = prototypeId;
    this.m_instanceId = instanceId;
    this.m_context.sessionId = TxApplication.ActiveDocument.eMSSessionId;
    this.m_loadedId = loadedId;
    if (loadedId > 0)
      this.LoadAttributesFromLoadedObject();
    else
      this.LoadAttributes();
  }

  protected override void SaveAttributes()
  {
    if (this.m_prototypeId <= 0 || this.m_instanceId <= 0 || !CApMFAdminUtils.ConfirmCheckOut(this.m_instanceId))
      return;
    EmpObjectKey empObjectKey1;
    empObjectKey1.objectId = this.m_instanceId;
    IEmpObject iempObject = (IEmpObject) new EmpObjectClass();
    EmpObjectKey empObjectKey2 = new EmpToolInstanceClass()[ref this.m_context, ref empObjectKey1];
    double[] numArray1 = new double[3];
    double[] numArray2 = new double[3];
    TxVector translation = this.Location.Translation;
    numArray1[0] = translation.X;
    numArray1[1] = translation.Y;
    numArray1[2] = translation.Z;
    TxVector rotationRpyXyz = this.Location.RotationRPY_XYZ;
    numArray2[0] = rotationRpyXyz.X;
    numArray2[1] = rotationRpyXyz.Y;
    numArray2[2] = rotationRpyXyz.Z;
    EmpNodeFieldsValues[] nodeFieldsValuesArray = new EmpNodeFieldsValues[2];
    EmpFieldValue[] empFieldValueArray1 = new EmpFieldValue[1];
    EmpFieldValue[] empFieldValueArray2 = new EmpFieldValue[2];
    IEmpNodesServices iempNodesServices = (IEmpNodesServices) new EmpNodesServicesClass();
    empFieldValueArray1[0].fieldName = "BasicUnitStructure";
    empFieldValueArray1[0].FieldValue = !this.BasicUnitStructure ? (object) 0 : (object) 1;
    empFieldValueArray2[0].fieldName = "location";
    empFieldValueArray2[0].FieldValue = (object) numArray1;
    empFieldValueArray2[1].fieldName = "rotation";
    empFieldValueArray2[1].FieldValue = (object) numArray2;
    nodeFieldsValuesArray[0].Node = empObjectKey1;
    nodeFieldsValuesArray[0].FieldsValues = empFieldValueArray1;
    nodeFieldsValuesArray[1].Node = empObjectKey2;
    nodeFieldsValuesArray[1].FieldsValues = empFieldValueArray2;
    iempNodesServices.SetFields(ref this.m_context, ref nodeFieldsValuesArray);
  }

  internal void LoadAttributesFromLoadedObject()
  {
    int instanceId = this.m_instanceId;
    this.m_instanceId = this.m_loadedId;
    this.LoadAttributes();
    this.m_instanceId = instanceId;
  }

  protected override void LoadAttributes()
  {
    if (this.m_prototypeId <= 0 && this.m_loadedId <= 0)
      return;
    IEmpNodesServices iempNodesServices = (IEmpNodesServices) new EmpNodesServicesClass();
    if (this.m_instanceId <= 0)
      return;
    EmpObjectKey empObjectKey1;
    empObjectKey1.objectId = this.m_instanceId;
    IEmpObject iempObject = (IEmpObject) new EmpObjectClass();
    EmpObjectKey empObjectKey2 = new EmpToolInstanceClass()[ref this.m_context, ref empObjectKey1];
    EmpObjectKey[] empObjectKeyArray = new EmpObjectKey[1]
    {
      empObjectKey1
    };
    string[] strArray = new string[1]
    {
      "BasicUnitStructure"
    };
    EmpFieldInfo[] fields = iempNodesServices.GetFields(ref this.m_context, ref empObjectKeyArray, ref strArray);
    EmpNodeFieldsValues[] nodeFieldsValuesArray = new EmpNodeFieldsValues[1];
    ArrayList arrayList = new ArrayList();
    if (fields.Length != 0 && fields[0].Value != null)
      this.BasicUnitStructure = (int) fields[0].Value == 1;
    EmpLocation empLocation = new EmpLayoutClass()[ref this.m_context, ref empObjectKey2];
    this.Location = new TxTransformation(new TxVector(empLocation.X, empLocation.Y, empLocation.Z), new TxVector(empLocation.RX, empLocation.RY, empLocation.RZ), (TxTransformation.TxRotationType) 1);
  }

  internal bool BasicUnitStructure
  {
    get => this.m_basicUnitStructure;
    set => this.m_basicUnitStructure = value;
  }

  internal TxTransformation Location
  {
    get => this.m_location;
    set => this.m_location = value;
  }
}
